package cn.bgs.service.impl;

import cn.bgs.pojo.VehicleArea;
import cn.bgs.pojo.VehicleMiddle;
import cn.bgs.common.R;
import cn.bgs.pageUtil.MybatisPageHelper;
import cn.bgs.pageUtil.PageResult;
import cn.bgs.pojo.VehicleNumber;
import cn.bgs.mapper.VehicleNumberMapper;
import cn.bgs.service.IVehicleNumberService;
import cn.bgs.vo.VehicleAdd;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import cn.bgs.vo.CarInfoVo;
import cn.bgs.vo.CwnumVo;
import cn.bgs.vo.UserVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

import java.util.List;

/**
 * <p>
 * 车位编号表 服务实现类
 * </p>
 *
 * @author cf
 * @since 2022-06-07
 */
@Service
public class VehicleNumberServiceImpl extends ServiceImpl<VehicleNumberMapper, VehicleNumber> implements IVehicleNumberService {
    @Autowired
    VehicleNumberMapper vehicleNumberMapper;
    Logger log= LogManager.getLogger();
    @Override
    public R selectnumInfo(CwnumVo cwnumVo) {
        MybatisPageHelper.startPage(cwnumVo.getPageRequest());
        PageResult pageResult = MybatisPageHelper.getPageResult(vehicleNumberMapper.selectnumInfo(cwnumVo));
        return R.ok().data("data",pageResult);
    }

    @Override
    public R selectAllUser(UserVo userVo) {
        List<UserVo> list = vehicleNumberMapper.selectAllUser(userVo);
        return R.ok().data("data",list);
    }




    @Override
    public List<VehicleNumber> appFindVehicleNumber(Integer vehicleAreaId) {
        String vehicleState="未绑定";
        QueryWrapper<VehicleNumber> wrapper = new QueryWrapper<>();


        wrapper.eq("vehicle_area_id",vehicleAreaId);
        wrapper.eq("vehicle_state",vehicleState);

        return list(wrapper);
    }

    @Override
    public List<VehicleAdd> appvehicleFind(Integer userId) {
        return vehicleNumberMapper.appvehicleFind(userId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteMyVehicle(Integer vehicleCarId) {
        try {
            VehicleMiddle vehicleMiddle=  vehicleNumberMapper.findVehicleNumId(vehicleCarId);
            vehicleNumberMapper.updateCarState(vehicleCarId);
            vehicleNumberMapper.updateNumState(vehicleMiddle.getVehicleNumberId());
            vehicleNumberMapper.deleteMyVehicle(vehicleCarId);

        }
        catch (Exception e){
            log.error(e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }




    }
    @Override
    public boolean insertPcInfo(CwnumVo cwnumVo) {
        boolean a = vehicleNumberMapper.insertPcInfo(cwnumVo);
        return a;
    }

    @Override
    public R delPcInfo(Integer vehicleNumberId) {
        int del = vehicleNumberMapper.delPcInfo(vehicleNumberId);
        return R.ok().data("data",del);
    }

    @Override
    public boolean updatePcInfo(CwnumVo cwnumVo) {
        int i = vehicleNumberMapper.updatePcInfo(cwnumVo);
        if (i>0){
            return true;
        }
        return false;
    }

    @Override
    public R PcInfo(CwnumVo cwnumVo) {
        List<CwnumVo> list = vehicleNumberMapper.PcInfo(cwnumVo);
        return R.ok().data("data",list);
    }

    @Override
    public R getPcarInfo(CarInfoVo carInfoVo) {
        List<CarInfoVo> list = vehicleNumberMapper.getPcarInfo();
        return R.ok().data("data",list);
    }
}
